package components.contorls.superClass
{
    import components.contorls.Alert;
    import components.core.UIComponent;
    import components.managers.PopUpManager;
    import components.utils.EventListenerUtil;
    
    import flash.events.Event;
    import flash.events.MouseEvent;

    [Event(name="close", type="flash.events.Event")]
    public class BaseAlert extends UIComponent
    {
		public static var defaultAlertClass:Class;
		
        public static const OK_BUTTON:int = 1;

        public static const NO_BUTTON:int = 2;

        private var _title:String = "Alert";

        public var _btnFlag:int = OK_BUTTON;

        private var _text:String = "";

        public var _closeHandlerFunction:Function;

        public function BaseAlert()
        {
            super();
			
			EventListenerUtil.addEventListener(okButton,MouseEvent.CLICK, okButton_mouseClickHandler);
			EventListenerUtil.addEventListener(noButton,MouseEvent.CLICK, noButton_mouseClickHandler);
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get titleLabel():BaseLabel
        {
            return getChildByName("titleLabelSkin") as BaseLabel;
        }

        public function get contentLabel():BaseLabel
        {
            return getChildByName("contentLabelSkin") as BaseLabel;
        }

        public function get okButton():BaseButton
        {
            return getChildByName("okButtonSkin") as BaseButton;
        }

        public function get noButton():BaseButton
        {
            return getChildByName("noButtonSkin") as BaseButton;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
			
			if (!_initialized)
			{
				if(titleLabel)
				{
					titleLabel.left=titleLabel.x;
					titleLabel.right=_normalWidth-titleLabel.x-titleLabel.width;
				}
				
				contentLabel.left=contentLabel.x;
				contentLabel.top=contentLabel.y;
				contentLabel.right=_normalWidth-contentLabel.x-contentLabel.width;
				contentLabel.bottom=_normalHeight-contentLabel.y-contentLabel.height;
				
				okButton.bottom=_normalHeight-okButton.y-okButton.height;
				okButton.right=_normalWidth-okButton.x-okButton.width;
				
				noButton.bottom=_normalHeight-noButton.y-noButton.height;
				noButton.right=_normalWidth-noButton.x-noButton.width;
			}
			
			if(titleLabel)
            	titleLabel.text = title;
			
            contentLabel.text = text;
        }

        protected override function measureSizes():Boolean
        {
			return super.measureSizes();
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();

            if (_btnFlag & OK_BUTTON)
                okButton.visible = true;
            else
                okButton.visible = false;

            if (_btnFlag & NO_BUTTON)
                noButton.visible = true;
            else
                noButton.visible = false;
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        [Inspectable(defaultValue="Alert")]
        public function get title():String
        {
            return _title;
        }

        public function set title(value:String):void
        {
            _title = value;
            invalidate();
        }

        [Inspectable(defaultValue="")]
        public function get text():String
        {
            return _text;
        }

        public function set text(value:String):void
        {
            _text = value;
            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/
        protected function okButton_mouseClickHandler(event:MouseEvent):void
        {
            if (_closeHandlerFunction != null)
                _closeHandlerFunction();
			
            closeWindow();
        }

        protected function noButton_mouseClickHandler(event:MouseEvent):void
        {
            closeWindow();
        }

        public function closeWindow():void
        {
            PopUpManager.removePopUp(this);

            dispatchEvent(new Event(Event.CLOSE));
			
			destroy();
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

			if(titleLabel)
            	titleLabel.focusComp = focusComp;
			
            contentLabel.focusComp = focusComp;
            okButton.focusComp = focusComp;
            noButton.focusComp = focusComp;
        }
		
		public static function show(info:String, title:String = "Alert", alertClass:Class=null, model:Boolean = true,
									btnFlag:int = 3, closeHandler:Function = null):BaseAlert
		{
			var alert:BaseAlert;
			if (alertClass)
				alert = new alertClass();
			else
				alert = new defaultAlertClass();
			
			alert.text = info;
			alert.title = title;
			alert._btnFlag = btnFlag;
			alert._closeHandlerFunction = closeHandler;
			
			alert.invalidate();
			
			PopUpManager.addPopUp(alert, model);
			PopUpManager.centerPopUp(alert);
			
			return alert;
		}
		
		public override function destroy():void
		{
			super.destroy();
			_title=null;
			_text=null;
			_closeHandlerFunction=null;
		}
    }
}